package org.wit.ecommerce.action.admin;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.wit.ecommerce.domain.Product;
import org.wit.ecommerce.dto.Navigator;
import org.wit.ecommerce.service.IProductService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Namespace("/admin/product")
public class ProductAction extends ActionSupport {
	@Autowired
	private IProductService productService;
	private int page = 1;
	private Navigator<Product> products;
	private Product product;
	private String name;
	private BigDecimal minPrice;
	private BigDecimal maxPrice;
	private Long id;
	
	@Action("list")
	public String list(){
		Map<String, Object> keywords = new HashMap<String, Object>();
		keywords.put("name", name);
		keywords.put("minPrice", minPrice);
		keywords.put("maxPrice", maxPrice);
		products = productService.getProductsByPage(page, keywords);
		return SUCCESS;
	}
	
	@Action("edit")
	public String edit(){
		product = productService.getProductById(id);
		return SUCCESS;
	}
	
	@Action(value="update", results={@Result(name="success", type="redirectAction", location="list")})
	public String update(){
		if(product.getId() != null){
			productService.updateProduct(product);
		} else {
			productService.createProduct(product);
		}
		return SUCCESS;
	}
	
	@Action(value="delete", results={@Result(name="success", type="redirectAction", location="list")})
	public String delete(){
		productService.deleteProduct(id);
		return SUCCESS;
	}
	
	@Action(value="add", results={@Result(name="success", location="/WEB-INF/jsp/admin/product/edit.jsp")})
	public String add(){
		return SUCCESS;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Navigator<Product> getProducts() {
		return products;
	}

	public void setProducts(Navigator<Product> products) {
		this.products = products;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getMinPrice() {
		return minPrice;
	}

	public void setMinPrice(BigDecimal minPrice) {
		this.minPrice = minPrice;
	}

	public BigDecimal getMaxPrice() {
		return maxPrice;
	}

	public void setMaxPrice(BigDecimal maxPrice) {
		this.maxPrice = maxPrice;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
